
<template>
    <div class="common_page">
        <div class="main_content" ref="mainContent">
            <el-card class="main-card" shadow="never">
                <el-form :model="searchData" class="page_form" ref="queryForm" :inline="true" label-width="68px" style="margin-bottom: -20px">
                    <el-form-item label="工单订单" prop="number">
                        <el-input placeholder="请输入内容" v-model="searchData.number"></el-input>
                    </el-form-item>
                    <el-form-item label="工单标题" prop="title">
                        <el-input placeholder="请输入内容" v-model="searchData.title"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" prop="state">
                        <el-select v-model="searchData.state" clearable placeholder="请选择">
                            <el-option v-for="item in buildList" :key="item.buildId" :label="item.buildName" :value="item.buildId" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="处理人" prop="conductorName">
                        <el-input placeholder="请输入内容" v-model="searchData.conductorName"></el-input>
                    </el-form-item>
                    <el-form-item label="" prop="templateNumber">
                        <el-button v-hasPermi="['productManage:workOrderManage:query']" type="primary" icon="el-icon-search" size="mini" @click="search">搜索</el-button>
                    </el-form-item>
                </el-form>
                <div class="card-toolbar mb8 flex-between" style="margin-top: 20px; width: 100%">
                    <el-button v-hasPermi="['productManage:workOrderManage:add']" type="primary" icon="el-icon-plus" size="mini" @click="addNew">新增</el-button>
                    <div>
                        <el-tooltip class="item" effect="dark" content="重置" placement="top">
                            <el-button size="mini" circle icon="el-icon-refresh" v-hasPermi="['productManage:workOrderManage:query']" @click="resetFields" />
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="导出" placement="top">
                            <el-button size="mini" circle icon="el-icon-download" v-hasPermi="['productManage:workOrderManage:export']" @click="handleExport" />
                        </el-tooltip>
                    </div>
                </div>

                <div class="page_content" style="width: 100%">
                    <div class="page_content_bottom">
                        <sysTable
                            :data="setTableData"
                            :columns="setTableColumns"
                            :curr-page="searchData.currPage"
                            :total-page="pageTotal"
                            :page-size="searchData.pageSize"
                            @sizeChange="sizeChange"
                            @currentChange="currentChange"
                        >
                            <template #state="scope">
                                <div :class="jumpClass(scope.scope.row.state)">{{ jumpState(scope.scope.row.state) }}</div>
                            </template>
                            <template #operation="scope">
                                <el-button v-hasPermi="['productManage:workOrderManage:edit']" size="small" type="primary" plain style="padding: 5px" icon="el-icon-edit" @click="showEdit(scope.scope.row)">
                                    编辑
                                </el-button>
                                <el-button v-hasPermi="['productManage:workOrderManage:detail']" size="small" type="primary" plain style="padding: 5px" icon="el-icon-view" @click="showDetail(scope.scope.row)">
                                    详情
                                </el-button>
                            </template>
                        </sysTable>
                    </div>
                </div>
            </el-card>
            <el-dialog :title="addOrEditTitle" :visible.sync="addOrEditDialog" width="700px" append-to-body>
                <el-form ref="form" :model="formData" label-width="90px">
                    <!-- <div style="opacity: 0;">1</div> -->

                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="工单标题:" prop="title">
                                <el-input v-model="formData.title" placeholder="请输入" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="问题分类:" prop="matterType">
                                <el-select v-model="formData.matterType" style="width: 265px" clearable placeholder="请选择">
                                    <el-option v-for="item in problemLists" :key="item.value" :label="item.name" :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="对应单号:" prop="type">
                                <div class="form_row">
                                    <el-select v-model="formData.type" clearable style="width: 265px" placeholder="请选择">
                                        <el-option v-for="item in orderTypeList" :key="item.id" :label="item.name" :value="item.id" />
                                    </el-select>
                                    <el-input v-model="formData.number" placeholder="请输入" style="width: 265px" />
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="指定处理人:" prop="deptId">
                                <div class="form_row">
                                    <el-select v-model="formData.deptId" clearable @change="getDeptPerson" style="width: 265px" placeholder="请选择">
                                        <el-option v-for="item in deptLists" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
                                    </el-select>
                                    <el-select v-model="formData.conductor" clearable style="width: 265px" placeholder="请选择">
                                        <el-option v-for="item in personList" :key="item.userId" :label="item.nickName" :value="item.userId" />
                                    </el-select>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="问题描述:" prop="matterDescribe">
                                <el-input v-model="formData.matterDescribe" type="textarea" rows="2" placeholder="请输入备注" style="width: 265px" />
                                <!-- <el-input v-model="formData.price"  placeholder="请输入单价" style="width:265px;" /> -->
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="图片:" prop="supplierName">
                                <!-- <div>最多可添加6张</div> -->
                                <ImageUpload :value="conversionUrl(formData.pictureList)" uploadAction="production/dispatchList/upload" @input="getImagePath($event)"></ImageUpload>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="submitForm">提 交</el-button>
                </div>
            </el-dialog>
            <el-dialog title="编辑" class="detail_form" :visible.sync="editDialog" width="700px" append-to-body>
                <el-form ref="form" :model="formData" label-width="90px">
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="工单标题:" prop="title">
                                <div class="detail_info">{{ formData.title }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="问题分类:" prop="matterType">
                                <div class="detail_info">{{ formData.matterName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="对应单号:" prop="type">
                                <div class="detail_info">{{ formData.typeName }} - {{ formData.number }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="指定处理人:" prop="deptId">
                                <div class="detail_info">{{ formData.deptName }} - {{ formData.conductorName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="问题描述:" prop="matterDescribe">
                                <div class="detail_info">{{ formData.matterDescribe }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="图片:" prop="supplierName">
                                <!-- <div>最多可添加6张</div> -->
                                <div class="dialog_img_list">
                                    <el-image v-for="(item, index) in formData.pictureList" :key="index" style="width: 116px; height: 116px; margin: 0 8px 0px 0px" :src="uploadImgUrl + item.imgUrl" fit="fill"></el-image>
                                </div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <div class="dialog_line"></div>
                    <div class="edit_doalog">
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="工单状态:" prop="supplierName">
                                    <el-radio-group v-model="formData.state">
                                        <el-radio :label="1">待处理</el-radio>
                                        <el-radio :label="2">处理中</el-radio>
                                        <el-radio :label="3">已完成</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="editSubmitForm">提 交</el-button>
                </div>
            </el-dialog>
            <el-dialog title="详情" :visible.sync="detailDialog" width="700px" append-to-body>
                <el-form ref="form" class="detail_form" :model="formData" label-width="90px">
                    <!-- <div style="opacity: 0;">1</div> -->

                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="工单标题:" prop="title">
                                <div class="detail_info">{{ formData.title }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="问题分类:" prop="matterType">
                                <div class="detail_info">{{ formData.matterName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="创建人:" prop="matterType">
                                <div class="detail_info">{{ formData.creatorName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="指定处理人:" prop="deptId">
                                <div class="detail_info">{{ formData.deptName }} - {{ formData.conductorName }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="对应单号:" prop="type">
                                <div class="detail_info">{{ formData.typeName }} - {{ formData.number }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="添加时间:" prop="type">
                                <div class="detail_info">{{ formData.addDate }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="催单次数:" prop="type">
                                <div class="detail_info">{{ formData.reminder || '-' }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="工单状态:" prop="matterDescribe">
                                <!-- <div class="detail_info">{{  }}</div> -->
                                <div class="special_info" :class="jumpClass(formData.state)">{{ jumpState(formData.state) }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="问题描述:" prop="matterDescribe">
                                <div class="detail_info">{{ formData.matterDescribe }}</div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="图片:" prop="supplierName">
                                <!-- <div>最多可添加6张</div> -->
                                <div class="dialog_img_list">
                                    <el-image v-for="(item, index) in formData.pictureList" :key="index" style="width: 116px; height: 116px; margin: 0 8px 0px 0px" :src="uploadImgUrl + item.imgUrl" fit="fill"></el-image>
                                </div>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>

                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="cancel">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import { getWorkOrderList, getProblemlist, getOrderTypelist, addWorkOrder, getWorkOrderDetail, editWorkOrder } from '../api/product/index';
import { listDept, listPerson } from '@/api/system/dept';
import sysTable from '../components/SysTable';
import ImageUpload from '@/components/ImageUpload';
export default {
    name: 'equipmentEnergy',
    components: {
        sysTable,
        ImageUpload,
    },
    data() {
        return {
            searchData: {
                conductorName: '',
                state: '',
                title: '',
                number: '',
                pageSize: 10,
                currPage: 1,
            },
            setTableData: [],
            pageTotal: 0,
            buildList: [
                {
                    buildName: '待处理',
                    buildId: 1,
                },
                {
                    buildName: '生产中',
                    buildId: 2,
                },
                {
                    buildName: '已完成',
                    buildId: 3,
                },
            ],
            problemLists: [],
            personList: [],
            deptLists: [],
            orderTypeList: [],
            setTableColumns: [
                {
                    label: '工单编号',
                    prop: 'number',
                    minWidth: '180',
                    align: 'center',
                    fixed: 'left',
                    showOverflowTooltip: true,
                },
                {
                    label: '工单标题',
                    prop: 'title',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '状态',
                    prop: 'state',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '问题分类',
                    prop: 'matterName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '问题描述',
                    prop: 'matterDescribe',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '添加时间',
                    prop: 'addDate',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '催单次数',
                    prop: 'reminder',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '创建人',
                    prop: 'creatorName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '处理人',
                    prop: 'conductorName',
                    width: '180',
                    align: 'center',
                    showOverflowTooltip: true,
                },
                {
                    label: '操作',
                    prop: 'operation',
                    minWidth: '120',
                    fixed: 'right',
                    align: 'center',
                },
            ],
            buttonLoading: false,
            formData: {
                title: '',
                type: '',
                matterType: '',
                deptId: '',
                number: '',
                conductor: '',
                matterDescribe: '',
                pictureList: [],
            },
            addOrEditTitle: '新增',
            addOrEditDialog: false,
            detailDialog: false,
            editDialog: false,
            uploadImgUrl: process.env.VUE_APP_BASE_API,
        };
    },
    mounted() {
        this.getPageList();
        this.getProblemlist();
        this.getDeptList();
        this.getOrderTypelist();
    },
    methods: {
        search() {
            this.buttonLoading = true;
            this.getPageList();
        },
        resetFields() {
            this.workShopList = [];
            this.deviceList = [];
            this.searchData = {
                buildingId: '',
                workshopId: null,
                deviceId: null,
                classifyId: '',
                deviceName: '',
                type: 1,
                date: ['2022-01-01', '2022-01-31'],
                pageSize: 10,
                currPage: 1,
            };
            this.search();
        },
        sizeChange(val) {
            this.searchData.pageSize = val;
            this.getPageList();
        },
        currentChange(val) {
            this.searchData.currPage = val;
            this.getPageList();
        },
        getPageList() {
            getWorkOrderList(this.searchData)
                .then((res) => {
                    console.log(res, '3322');
                    this.setTableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .finally(() => {
                    this.buttonLoading = false;
                });
        },
        getProblemlist() {
            getProblemlist().then((res) => {
                this.problemLists = res || [];
            });
        },
        getDeptList() {
            const obj = {};
            listDept(obj).then((res) => {
                this.deptLists = res.data || [];
            });
        },
        getDeptPerson(val) {
            listPerson(val).then((res) => {
                this.personList = res.data || [];
            });
        },
        getOrderTypelist() {
            getOrderTypelist().then((res) => {
                this.orderTypeList = res || [];
            });
        },
        handleExport() {
            this.buttonLoading = true;
            var datas = {
                conductorName: this.searchData.conductorName,
                state: this.searchData.state,
                title: this.searchData.title,
                number: this.searchData.number,
                pageSize: '',
                currPage: '',
            };
            const obj = JSON.parse(JSON.stringify(datas));
            this.download('production/dispatchList/queryListReport', obj, '工单记录.xlsx');
            this.buttonLoading = false;
            // exportBuild(obj).then(res=> {
            //   const fileName = '设备能源监控记录.xlsx'
            //   saveExcel(res.data, fileName)
            // })
        },
        getImagePath(val) {
            var arr = val.split(',') || [];
            this.formData.pictureList = [];
            arr.map((item) => {
                const obj = {
                    imgUrl: item,
                };
                this.formData.pictureList.push(obj);
            });
        },
        conversionUrl(val) {
            var urls = '';
            val.map((item, index) => {
                if (val.length == index + 1) {
                    urls = urls + item.imgUrl;
                } else {
                    urls = urls + item.imgUrl + ',';
                }
            });
            return urls;
        },
        addNew() {
            this.addOrEditTitle = '新增';
            this.addOrEditDialog = true;
        },
        showDetail(val) {
            getWorkOrderDetail(val.id).then((res) => {
                this.formData = res.data;
                this.detailDialog = true;
            });
            // console.log(val, '详情')
        },
        showEdit(val) {
            getWorkOrderDetail(val.id).then((res) => {
                console.log(res, '333');
                this.formData = res.data;
                this.editDialog = true;
            });
        },
        cancel() {
            this.formData = {
                title: '',
                type: '',
                matterType: '',
                deptId: '',
                number: '',
                conductor: '',
                matterDescribe: '',
                pictureList: [],
            };
            this.addOrEditDialog = false;
            this.detailDialog = false;
            this.editDialog = false;
        },
        submitForm() {
            addWorkOrder(this.formData).then((res) => {
                console.log(res, '333');
                if (res.code == 200) {
                    this.searchData.currPage = 1;
                    this.getPageList();
                    this.cancel();
                } else {
                    this.$message.error('新增失败，请联系管理员');
                }
            });
            // this.addOrEditDialog = false
        },
        editSubmitForm() {
            editWorkOrder(this.formData).then((res) => {
                if (res.code == 200) {
                    this.searchData.currPage = 1;
                    this.getPageList();
                    this.cancel();
                } else {
                    this.$message.error('编辑失败，请联系管理员');
                }
            });
        },
        jumpState(val) {
            switch (val) {
                case 1:
                    return '待处理';
                case 2:
                    return '处理中';
                case 3:
                    return '已完成';
                default:
                    return '-';
            }
        },
        jumpClass(val) {
            switch (val) {
                case 1:
                    return 'waits';
                case 2:
                    return 'processs';
                case 3:
                    return 'completes';
                default:
                    return '-';
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.common_page {
    height: 100%;
    width: 100%;
    background: #f3f4f5;
    overflow: auto;
}
.main_content {
    padding: 16px 20px;
}
::v-deep.el-card {
    border-radius: 8px;
}
.page_content {
    background: #fff;
    padding-bottom: 30px;
    box-sizing: border-box;
    padding: 20px 20px 30px 20px;
    border-radius: 8px;
    .page_content_head {
        display: flex;
    }
}
::v-deep.el-form {
    .el-form-item {
        margin-bottom: 20px;
        .el-form-item__label {
            // line-height: 20px;
            text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}
::v-deep.detail_form {
    .el-form-item {
        margin-bottom: 20px;
        .el-form-item__label {
            line-height: 20px !important;
            text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}

.edit_doalog {
    ::v-deep.el-form-item {
        // margin-bottom: 20px;
        .el-form-item__label {
            line-height: 36px;
            text-align: left;
            color: #666;
            font-size: 14px;
            font-weight: 400;
        }
    }
}
::v-deep.sys_table {
    .tableHeaderRow {
        cursor: pointer;
        .tableHeaderCell:active {
            opacity: 0.6;
        }
        .tableHeaderCell:last-of-type:active {
            opacity: 1;
        }
        .tableHeaderCell:first-of-type:active {
            opacity: 1;
        }
        .tableHeaderCell:last-of-type {
            cursor: default;
        }
        .tableHeaderCell:first-of-type {
            cursor: default;
        }
    }
}
.ope_btn {
    width: 44px;
    height: 28px;
    line-height: 28px;
    color: #3399ff;
    font-size: 14px;
    background: #ffffff;
    border-radius: 4px 4px 4px 4px;
    border: 1px solid rgba(0, 85, 255, 0.5);
    padding: 0px;
}
.del_btn {
    width: 44px;
    height: 28px;
    background: #ffffff;
    line-height: 28px;
    color: #ff4c4d;
    font-size: 14px;
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    border: 1px solid rgba(255, 76, 77, 0.5);
    padding: 0px;
}

.processs {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(255, 119, 51, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #ff7733;
    font-size: 14px;
    margin: 0 auto;
}

.waits {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(51, 153, 255, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #3399ff;
    font-size: 14px;
    margin: 0 auto;
}

.completes {
    width: 58px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    background: rgba(0, 178, 74, 0.1);
    border-radius: 4px 4px 4px 4px;
    color: #00b24a;
    font-size: 14px;
    margin: 0 auto;
}

.form_row {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.detail_info {
    font-size: 14px;
    line-height: 20px;
    color: #333;
}
.special_info {
    height: 20px;
    line-height: 20px;
    margin: 0px 0px;
}
.dialog_line {
    height: 1px;
    width: 100%;
    background: rgba(0, 0, 0, 0.08);
    margin-bottom: 20px;
}
</style>
